package com.util.config.validator;

/**
 * Copyright
 *
 * @Author WuHuang
 * @Date 2021/11/4
 */
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;

public class CheckEnumValidator implements ConstraintValidator<CheckEnum, Object> {
    private static final Logger log = LoggerFactory.getLogger(CheckEnumValidator.class);
    private Set<Object> validValues = new HashSet();

    public CheckEnumValidator() {
    }

    public void initialize(CheckEnum annotation) {
        Class<?> enumClass = annotation.enumClass();
        String enumPath = annotation.enumPath();
        String enumField = annotation.enumField();

        try {
            if (Enum.class.equals(enumClass)) {
                enumClass = Class.forName(enumPath);
            }

            Object[] objs = enumClass.getEnumConstants();
            Method method = enumClass.getMethod(enumField);
            Object[] var7 = objs;
            int var8 = objs.length;

            for(int var9 = 0; var9 < var8; ++var9) {
                Object obj = var7[var9];
                Object code = method.invoke(obj);
                this.validValues.add(code);
            }

            log.info("enumClass:{} validValues:{}", enumClass, this.validValues);
        } catch (Exception var12) {
//            log.error(MonitorUtil.getErrorMsg(var12.getClass().getSimpleName(), "Validator:{} initialize Error!"), var12);
        }

    }

    public boolean isValid(Object value, ConstraintValidatorContext context) {
        return value == null ? true : this.validValues.contains(value);
    }
}
